home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / source / src / arith / imem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.9 KB  |  206 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  imem.c
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. /*        imem.c        RD, 05.04.90    */
  16.  
  17. #include <LEDA/impl/iint.h>
  18. #include <LEDA/impl/iloc.h>
  19.  
  20. #define I_STATISTICS
  21.  
  22. _VOID_ Iprint_statistics(_VOID_)
  23. {
  24. }
  25.  
  26. char * Imalloc(i)
  27.     int i;
  28. {    return (char*)MYMALLOC(i);
  29. }
  30.  
  31. void Ifree(u)
  32.     char * u;
  33. {    MYFREE(u);
  34. }
  35.  
  36. /*#define MEMLISTMAX 16*/
  37. #define MEMLISTMAX 32
  38. #define MEMLISTMIN 2
  39.  
  40. typedef union uMemEl {
  41.         union uMemEl *next;
  42.         PLACE mem;
  43. } MemEl, *pMemEl;
  44.  
  45. typedef struct sMemList {
  46.         pMemEl free;
  47.         int size;
  48. } tMemList;
  49.  
  50. static tMemList MemList[MEMLISTMAX];
  51. static BOOLEAN MemListInit=FALSE;
  52.  
  53. #ifdef I_STATISTICS
  54. static long    Ivec_used[MEMLISTMAX];
  55. static long    Ivec_allocated[MEMLISTMAX];
  56. #endif
  57.  
  58. PLACE * newvec(maxl)
  59.         int *maxl;
  60. {       register int i; 
  61.         register pMemEl u;
  62.         register int a, ml;
  63.         if (!MemListInit) {
  64.                 int j=2;
  65.         MemListInit=TRUE;
  66.                 for (i=1; i<MEMLISTMAX; i++) {
  67.                         MemList[i].size=j;
  68.             MemList[i].free=NULL;
  69.                         j<<=1;
  70. #ifdef I_STATISTICS
  71.             Ivec_used[i]=0;
  72.             Ivec_allocated[i]=0;
  73. #endif
  74.                 }
  75.     }
  76.         a=*maxl;
  77.         i=MEMLISTMIN;
  78.     if (a) {
  79.         a--;
  80.         a>>=i;
  81.         while(a) {
  82.                 a>>=1;
  83.                 i++;
  84.         }
  85.         if (i>=MEMLISTMAX)
  86.         Ierror("newvec: exceeded MEMLISTMAX\n");
  87.     }
  88.         ml=MemList[i].size;
  89.         *maxl=ml;
  90.         u=MemList[i].free;
  91.         if (u) {
  92.                 MemList[i].free=u->next;
  93. #ifdef I_STATISTICS
  94.         Ivec_used[i]++;
  95. #endif
  96.                 return (pPLACE)u;
  97.         } else {
  98.                 u=(pMemEl)MYMALLOC(ml*sizeof(PLACE));
  99.         if (!u) {
  100.             int j;
  101.             for (j=1; j<MEMLISTMAX; j++) {
  102.             while (u=MemList[j].free) {
  103.                 MemList[j].free=u->next;
  104.                 MYFREE((char *)u);
  105.             }
  106.             }
  107.             u=(pMemEl)MYMALLOC(ml*sizeof(PLACE));
  108.             if(!u) {
  109. #ifdef I_STATISTICS
  110.             Iprint_statistics();
  111. #endif
  112.             Ierror("newvec: memory full\n");
  113.             }
  114.         }
  115. #ifdef I_STATISTICS
  116.         Ivec_used[i]++;
  117.         Ivec_allocated[i]++;
  118. #endif
  119.                 return (pPLACE)u;
  120. }       }               /* newvec */
  121.  
  122. void delvec(u, maxl)
  123.         PLACE * u; register int maxl;
  124. {       register int i;
  125.     register pMemEl v;
  126.     v=(pMemEl) u;
  127.         i=MEMLISTMIN;
  128.     maxl--;
  129.     maxl>>=i;
  130.     while(maxl) {
  131.                 maxl>>=1;
  132.                 i++;
  133.         }
  134.         v->next=MemList[i].free;
  135.         MemList[i].free=v;
  136. #ifdef I_STATISTICS
  137.     Ivec_used[i]--;
  138. #endif
  139. }        /* delvec */
  140.  
  141. /*************************************/
  142.  
  143. static Integer * Ifreelist=NULL;
  144.  
  145. #ifdef I_STATISTICS
  146. static long    Ihead_used=0;
  147. static long    Ihead_allocated=0;
  148. #endif
  149.  
  150. Integer * newInteger()
  151. {       register Integer * u;
  152.     if (Ifreelist) {
  153.         u=Ifreelist;
  154.         Ifreelist=(Integer *)(u->vec);
  155. #ifdef I_STATISTICS
  156.         Ihead_used++;
  157. #endif
  158.         return u;
  159.     } else {
  160.         u=(Integer *) MYMALLOC(sizeof(Integer));
  161.         if (!u) {
  162. #ifdef I_STATISTICS
  163.             Iprint_statistics();
  164. #endif
  165.             Ierror("newInteger: memory full\n");
  166.         }
  167. #ifdef I_STATISTICS
  168.         Ihead_used++;
  169.         Ihead_allocated++;
  170. #endif
  171.         return u;
  172.     }
  173. }        /* newInteger */
  174.  
  175. void delInteger(u)
  176.     register Integer *u;
  177. {    u->vec = (PLACE *) Ifreelist;
  178.     Ifreelist=u;
  179. #ifdef I_STATISTICS
  180.         Ihead_used--;
  181. #endif
  182. }        /* delInteger */
  183.  
  184. /*
  185. #ifdef I_STATISTICS
  186. _VOID_ Iprint_statistics(_VOID_)
  187. {    int i;
  188.     fprintf(stderr,"\nInteger memory management statistics:\n");
  189.     fprintf(stderr,"Integer structs: %ld allocated, %ld used.\n",
  190.         Ihead_used, Ihead_allocated);
  191.     fprintf(stderr,
  192.         "  size(PLACEs)  size(bytes)   allocated     used\n");
  193.     for (i=1; i<MEMLISTMAX; i++) {
  194.         fprintf(stderr, "%10d    %10d    %10ld    %10ld\n",
  195.             MemList[i].size, MemList[i].size*sizeof(PLACE),
  196.             Ivec_allocated[i], Ivec_used[i]);
  197.     }
  198. }
  199. #else
  200. _VOID_ Iprint_statistics(_VOID_)
  201. {
  202. }
  203. #endif
  204. */
  205.